﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.MapInfo.Model;
using System.IO;

public partial class MapInfo_DepartmentIntro : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindParentDroptDownd();
            ShowInfo();
        }
    }
    private void BindParentDroptDownd()
    {
        string DepartmentID = Request["DepartmentID"];
        string strSql = string.Empty;
        string tempID = string.Empty;
        if (DepartmentID.Substring(4, 1).Equals("3"))
        {
            strSql = "Left(DepartmentID,4)='"+DepartmentID.Substring(0,4)+"' And DepartmentID<>'"+DepartmentID+"' And SubString(DepartmentID,5,1)<>'3'";
            
        }
        else if (DepartmentID.Substring(4, 1).Equals("2"))
        {
            strSql = "Right(DepartmentID,3)='000' And DepartmentID<>'1300000'";
          
        }
        else
        {
            strSql = "Right(DepartmentID,3)='000'And DepartmentID<>'" + DepartmentID + "'";
        }
        List<Yipusoft.Account.Model.DepartmentInfo> list= Yipusoft.Account.BLL.Department.GetList(strSql);
        this.dptParentDepartmentID.DataSource = list;
        this.dptParentDepartmentID.DataTextField = "FullName";
        this.dptParentDepartmentID.DataValueField = "DepartmentID";
        this.dptParentDepartmentID.DataBind();       
        this.dptParentDepartmentID.SelectedValue = Request["DepartmentID"].Substring(0, 4) + "000";
        txtShortName.Text = Yipusoft.Account.BLL.Department.GetDepartment(Request["DepartmentID"]).ShortName;
       

    }
    private void ShowInfo()
    {
        if (Request["DepartmentID"] == null)
            return;


        string DepartmentID = Request["DepartmentID"];
        Bind(DepartmentID);
        Yipusoft.MapInfo.Model.DepartmentIntroInfo model = Yipusoft.MapInfo.BLL.DepartmentIntroduce.GetModel(DepartmentID);
        if (model != null)
        {
            this.txtDepartmentAddress.Text = model.DepartmentAddress;
            this.txtCreateDate.Text = model.CreateDate.ToString();
            this.txtDepartmentAreaSize.Text = model.DepartmentAreaSize.ToString();
            this.txtBuildAreaSize.Text = model.BuildAreaSize.ToString();
            this.txtPeopleCount.Text = model.PeopleCount.ToString();
            this.txtOfficerCount.Text = model.OfficerCount.ToString();
            this.txtContractCount.Text = model.ContractCount.ToString();
            this.txtEmployeeCount.Text = model.EmployeeCount.ToString();
            this.txtCarbarnCount.Text = model.CarbarnCount.ToString();
            this.txtDutyCarCount.Text = model.DutyCarCount.ToString();
            this.txtDutyCarIntro.Text = model.DutyCarIntro;
            this.txtTaskDate.Text = model.TaskDate.ToString();
            this.txtProtectCount.Text = model.ProtectCount.ToString();
            this.txtMangerDepartment.Text = model.MangerDepartment;
            this.txtFloorUse.Text = model.FloorUse;
            this.DptType.SelectedValue = model.DepartmentType;
            this.txtCadreCount.Text = model.CadreCount.ToString();
            this.txtparentDepartment.Text = model.ParentDepartment;
            this.txtLatitude.Text = model.Latitude.ToString();
            this.txtLongitude.Text = model.Longitude.ToString();
            if (!string.IsNullOrEmpty(model.ShortName))
            {
                this.txtShortName.Text = model.ShortName;
            }
            if (!string.IsNullOrEmpty(model.ParentID))
            {
            
                this.dptParentDepartmentID.SelectedValue = model.ParentID;
            }
        }
        else
        {
            Bind("");
        }

    }

    private void Bind(string DepartmentID)
    {
        this.DptDepartment1.DataSource = Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,3)='000'");
        this.DptDepartment1.DataTextField = "FullName";
        this.DptDepartment1.DataValueField = "DepartmentID";
        this.DptDepartment1.DataBind();
        if (DepartmentID.Trim() != "")
        {

            this.DptDepartment2.DataSource = Yipusoft.Account.BLL.Department.GetList(" Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "'");
            this.DptDepartment2.DataTextField = "FullName";
            this.DptDepartment2.DataValueField = "DepartmentID";
            this.DptDepartment2.DataBind();
            this.DptDepartment1.SelectedValue = DepartmentID.Substring(0, 4) + "000";
            this.DptDepartment2.SelectedValue = DepartmentID;

        };
        DptType_SelectedIndexChanged(this.DptType, new EventArgs());

    }
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        lblPromt.Text = "";
        try
        {
            Decimal temp = Decimal.Parse(txtLongitude.Text);
            Decimal temp1 = Decimal.Parse(txtLatitude.Text);
        }
        catch
        {
            lblPromt.Text = "经度或纬度输入格式不正确";
            return;
        }
        //if (Request["DepartmentID"] == null)
        //    return;
        //string photoUrl = "";
        //int _limitwidth = 800;
        //int _limitheight = 600;

        //lblPromt.Text = "";

        //if (FilePic.PostedFile != null && FilePic.PostedFile.ContentLength > 0)
        //{
        //    {
        //        // if not already present, create a directory named /Uploads/<CurrentUserName>
        //        string filename = FilePic.FileName.ToLower();
        //        int i = filename.LastIndexOf(".");
        //        filename = filename.Substring(i, filename.Length - i);

        //        if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
        //        {

        //            lblPromt.Text = "不受支持的类型,请重新选择!";
        //            return;

        //        }//检查上传文件的格式是否有效
        //        else
        //        {

        //            Stream oStream = FilePic.PostedFile.InputStream;
        //            System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

        //            int owidth = oImage.Width; //原图宽度
        //            int oheight = oImage.Height; //原图高度

        //            if (owidth > _limitwidth || oheight > _limitheight)
        //            {

        //                lblPromt.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
        //                return;

        //            }//检查是否超出规定尺寸
        //            else
        //            {
        //                string dirUrl = this.Request.ApplicationPath +
        //                   "Uploads/" + Request["departmentID"];
        //                string dirPath = Server.MapPath(dirUrl);
        //                if (!Directory.Exists(dirPath))
        //                    Directory.CreateDirectory(dirPath);
        //                // save the file under the user's personal folder
        //                string fileUrl = dirUrl + "/" + DateTime.Now.ToString("ssyyyyMMddhhmmms") + filename;
        //                FilePic.PostedFile.SaveAs(Server.MapPath(fileUrl));
        //                photoUrl = fileUrl;
        //            }
        //        }
        //    }
        //    string DepartmentID = Request["DepartmentID"];

        //    Yipusoft.MapInfo.Model.DepartmentIntroInfo model = Yipusoft.MapInfo.BLL.DepartmentIntroduce.GetModel(DepartmentID);

        //    try
        //    {
        //        string DepartmentIDF = DepartmentID;
        //        string DepartmentName = DptDepartment2.SelectedItem.Text;
        //        string DepartmentAddress = this.txtDepartmentAddress.Text;
        //        DateTime CreateDate = DateTime.Parse(this.txtCreateDate.Text.Trim() != "" ? txtCreateDate.Text.Trim() : DateTime.MaxValue.ToShortDateString());
        //        decimal DepartmentAreaSize = decimal.Parse(this.txtDepartmentAreaSize.Text.Trim() != "" ? txtDepartmentAreaSize.Text.Trim() :"1.00");
        //        decimal BuildAreaSize = decimal.Parse(this.txtBuildAreaSize.Text.Trim() != "" ? txtBuildAreaSize.Text.Trim() : "1.00");
        //        int PeopleCount = int.Parse(this.txtPeopleCount.Text.Trim() != "" ? txtPeopleCount.Text.Trim() : "0");
        //        int OfficerCount = int.Parse(this.txtOfficerCount.Text.Trim() != "" ? txtOfficerCount.Text : "0");
        //        int ContractCount = int.Parse(this.txtContractCount.Text.Trim() != "" ? txtContractCount.Text.Trim() : "0");
        //        int EmployeeCount = int.Parse(this.txtEmployeeCount.Text.Trim() != "" ? txtEmployeeCount.Text.Trim() : "0");
        //        int CarbarnCount = int.Parse(this.txtCarbarnCount.Text.Trim() != "" ? txtCarbarnCount.Text.Trim() : "0");
        //        int DutyCarCount = int.Parse(this.txtDutyCarCount.Text.Trim() != "" ? txtDutyCarCount.Text.Trim() : "0");
        //        string DutyCarIntro = this.txtDutyCarIntro.Text;
        //        DateTime TaskDate = DateTime.Parse(this.txtTaskDate.Text.Trim() != "" ? txtTaskDate.Text.Trim() : DateTime.MaxValue.ToShortDateString());
        //        decimal ProtectCount = decimal.Parse(this.txtProtectCount.Text.Trim() != "" ? txtProtectCount.Text.Trim() : "1.00");
        //        string MangerDepartment = this.txtMangerDepartment.Text;
        //        string FloorUse = this.txtFloorUse.Text;
        //        string Picture = photoUrl;
        //        int CadreCount = int.Parse(txtCadreCount.Text.Trim() != "" ? txtCadreCount.Text : "0");
        //        string ParentDepartment = txtparentDepartment.Text.Trim() != "" ? txtparentDepartment.Text : "";



        //        if (model == null)
        //            model = new DepartmentIntroInfo();

        //        model.DepartmentID = DepartmentID;
        //        model.DepartmentName = DepartmentName;
        //        model.DepartmentAddress = DepartmentAddress;
        //        model.CreateDate = CreateDate;
        //        model.DepartmentAreaSize = DepartmentAreaSize;
        //        model.BuildAreaSize = BuildAreaSize;
        //        model.PeopleCount = PeopleCount;
        //        model.OfficerCount = OfficerCount;
        //        model.ContractCount = ContractCount;
        //        model.EmployeeCount = EmployeeCount;
        //        model.CarbarnCount = CarbarnCount;
        //        model.DutyCarCount = DutyCarCount;
        //        model.DutyCarIntro = DutyCarIntro;
        //        model.TaskDate = TaskDate;
        //        model.ProtectCount = ProtectCount;
        //        model.MangerDepartment = MangerDepartment;
        //        model.FloorUse = FloorUse;
        //        model.Picture = Picture;
        //        model.DepartmentType = DptType.SelectedValue;
        //        model.CadreCount = CadreCount;
        //        model.ParentDepartment = ParentDepartment;

        //        Yipusoft.MapInfo.Model.DepartmentIntroInfo model1 = Yipusoft.MapInfo.BLL.DepartmentIntroduce.GetModel(DepartmentID);
        //        int result = 0;
        //        if (model1 == null)
        //        {
        //           result= Yipusoft.MapInfo.BLL.DepartmentIntroduce.Add(model);
        //        }
        //        else
        //        {
        //            result = Yipusoft.MapInfo.BLL.DepartmentIntroduce.Update(model);
        //        }
        //        if (result > 0)
        //            lblPromt.Text = "保存成功";
        //        else
        //            lblPromt.Text = "保存失败";
        //    }
        //    catch (Exception ex)
        //    {
        //        lblPromt.Text = ex.Message;
        //    };
        //}


        if (Request["DepartmentID"] == null)
            return;
        string photoUrl = "";
        int _limitwidth = 800;
        int _limitheight = 600;

        lblPromt.Text = "";
        if (FilePic.PostedFile != null && FilePic.PostedFile.ContentLength > 0)
        {

            // if not already present, create a directory named /Uploads/<CurrentUserName>
            string filename = FilePic.FileName.ToLower();
            int i = filename.LastIndexOf(".");
            filename = filename.Substring(i, filename.Length - i);

            if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
            {

                lblPromt.Text = "不受支持的类型,请重新选择!";
                return;

            }//检查上传文件的格式是否有效
            else
            {

                Stream oStream = FilePic.PostedFile.InputStream;
                System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

                int owidth = oImage.Width; //原图宽度
                int oheight = oImage.Height; //原图高度

                if (owidth > _limitwidth || oheight > _limitheight)
                {

                    lblPromt.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
                    return;

                }//检查是否超出规定尺寸
                else
                {
                    string dirUrl = this.Request.ApplicationPath +
                       "Uploads/" + Request["departmentID"];
                    string dirPath = Server.MapPath(dirUrl);
                    if (!Directory.Exists(dirPath))
                        Directory.CreateDirectory(dirPath);
                    // save the file under the user's personal folder
                    string fileUrl = dirUrl + "/" + DateTime.Now.ToString("ssyyyyMMddhhmmms") + filename;
                    FilePic.PostedFile.SaveAs(Server.MapPath(fileUrl));
                    photoUrl = fileUrl;
                }
            }
        }

        string DepartmentID = Request["DepartmentID"];

        Yipusoft.MapInfo.Model.DepartmentIntroInfo model = Yipusoft.MapInfo.BLL.DepartmentIntroduce.GetModel(DepartmentID);
        string strpic = model != null ? model.Picture.Trim() : "";
        try
        {
            string DepartmentIDF = DepartmentID;
            string DepartmentName = DptDepartment2.SelectedItem.Text;
            string DepartmentAddress = this.txtDepartmentAddress.Text;
            DateTime CreateDate = DateTime.Parse(this.txtCreateDate.Text.Trim() != "" ? txtCreateDate.Text.Trim() : DateTime.MaxValue.ToShortDateString());
            decimal DepartmentAreaSize = decimal.Parse(this.txtDepartmentAreaSize.Text.Trim() != "" ? txtDepartmentAreaSize.Text.Trim() : "1.00");
            decimal BuildAreaSize = decimal.Parse(this.txtBuildAreaSize.Text.Trim() != "" ? txtBuildAreaSize.Text.Trim() : "1.00");
            int PeopleCount = int.Parse(this.txtPeopleCount.Text.Trim() != "" ? txtPeopleCount.Text.Trim() : "0");
            int OfficerCount = int.Parse(this.txtOfficerCount.Text.Trim() != "" ? txtOfficerCount.Text : "0");
            int ContractCount = int.Parse(this.txtContractCount.Text.Trim() != "" ? txtContractCount.Text.Trim() : "0");
            int EmployeeCount = int.Parse(this.txtEmployeeCount.Text.Trim() != "" ? txtEmployeeCount.Text.Trim() : "0");
            int CarbarnCount = int.Parse(this.txtCarbarnCount.Text.Trim() != "" ? txtCarbarnCount.Text.Trim() : "0");
            int DutyCarCount = int.Parse(this.txtDutyCarCount.Text.Trim() != "" ? txtDutyCarCount.Text.Trim() : "0");
            string DutyCarIntro = this.txtDutyCarIntro.Text;
            DateTime TaskDate = DateTime.Parse(this.txtTaskDate.Text.Trim() != "" ? txtTaskDate.Text.Trim() : DateTime.MaxValue.ToShortDateString());
            decimal ProtectCount = decimal.Parse(this.txtProtectCount.Text.Trim() != "" ? txtProtectCount.Text.Trim() : "1.00");
            string MangerDepartment = this.txtMangerDepartment.Text;
            string FloorUse = this.txtFloorUse.Text;
            string Picture = FilePic.FileName.Trim() != "" ? photoUrl : strpic;
            int CadreCount = int.Parse(txtCadreCount.Text.Trim() != "" ? txtCadreCount.Text : "0");
            string ParentDepartment = txtparentDepartment.Text.Trim() != "" ? txtparentDepartment.Text : "";



            if (model == null)
                model = new DepartmentIntroInfo();

            model.DepartmentID = DepartmentID;
            model.DepartmentName = DepartmentName;
            model.DepartmentAddress = DepartmentAddress;
            model.CreateDate = CreateDate;
            model.DepartmentAreaSize = DepartmentAreaSize;
            model.BuildAreaSize = BuildAreaSize;
            model.PeopleCount = PeopleCount;
            model.OfficerCount = OfficerCount;
            model.ContractCount = ContractCount;
            model.EmployeeCount = EmployeeCount;
            model.CarbarnCount = CarbarnCount;
            model.DutyCarCount = DutyCarCount;
            model.DutyCarIntro = DutyCarIntro;
            model.TaskDate = TaskDate;
            model.ProtectCount = ProtectCount;
            model.MangerDepartment = MangerDepartment;
            model.FloorUse = FloorUse;
            model.Picture = Picture;
            model.DepartmentType = DptType.SelectedValue;
            model.CadreCount = CadreCount;
            model.ParentDepartment = ParentDepartment;
            model.ParentID = dptParentDepartmentID.SelectedValue;
            model.Longitude = Decimal.Parse(txtLongitude.Text);
            model.Latitude = Decimal.Parse(txtLatitude.Text);
            model.TimeLastModified = DateTime.Now;
            model.ShortName = txtShortName.Text;
            Yipusoft.MapInfo.Model.DepartmentIntroInfo model1 = Yipusoft.MapInfo.BLL.DepartmentIntroduce.GetModel(DepartmentID);
            int result = 0;
            if (model1 == null)
            {
                result = Yipusoft.MapInfo.BLL.DepartmentIntroduce.Add(model);
            }
            else
            {
                result = Yipusoft.MapInfo.BLL.DepartmentIntroduce.Update(model);
            }
            if (result > 0)
                lblPromt.Text = "保存成功";
            else
                lblPromt.Text = "保存失败";
        }
        catch (Exception ex)
        {
            lblPromt.Text = ex.Message;
        };
    }
    protected void DptType_SelectedIndexChanged(object sender, EventArgs e)
    {

        txtMangerDepartment.Visible = DptType.SelectedValue.Equals("Type03");



        txtContractCount.Visible = !DptType.SelectedValue.Equals("Type02");
        txtEmployeeCount.Visible = !DptType.SelectedValue.Equals("Type02");
        txtContractCount.Visible = !DptType.SelectedValue.Equals("Type02");

        txtProtectCount.Visible = !DptType.SelectedValue.Equals("Type03");
        txtDutyCarIntro.Visible = !DptType.SelectedValue.Equals("Type03");
        txtDutyCarIntro.Visible = !DptType.SelectedValue.Equals("Type03");
        txtOfficerCount.Visible = !DptType.SelectedValue.Equals("Type03");
        txtDepartmentAreaSize.Visible = !DptType.SelectedValue.Equals("Type03");
        txtContractCount.Visible = DptType.SelectedValue.Equals("Type01");
        txtTaskDate.Visible = !DptType.SelectedValue.Equals("Type03");
        txtFloorUse.Visible = !DptType.SelectedValue.Equals("Type03");
        txtDutyCarCount.Visible = !DptType.SelectedValue.Equals("Type03");
        txtCarbarnCount.Visible = !DptType.SelectedValue.Equals("Type03");
        txtparentDepartment.Visible = !DptType.SelectedValue.Equals("Type03");
        lblpm1.Visible = lblpm.Visible = lblpm3.Visible = !DptType.SelectedValue.Equals("Type03");
        txtparentDepartment.Visible = !DptType.SelectedValue.Equals("Type04");

    }
    protected void DptDepartment1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DptDepartment1.SelectedValue.Trim() != "")
        {
            this.DptDepartment2.DataSource = Yipusoft.Account.BLL.Department.GetList(" Left(DepartmentID,4)='" + DptDepartment1.SelectedValue.Substring(0, 4) + "'");
            this.DptDepartment2.DataTextField = "FullName";
            this.DptDepartment2.DataValueField = "DepartmentID";
            this.DptDepartment2.DataBind();

        };

    }
}
